﻿Imports SlimDX
Imports SlimDX.Direct3D9


Public Class ShaderEnvironment
    Implements IShader

    Enum ShaderType As Short
        Normal = 0
        Blended = 1
        BlendedSpecular = 2
    End Enum

    Dim Technique As EffectHandle

    Dim BaseMap As Texture
    Dim PrimaryDetail As Texture
    Dim SecondaryDetail As Texture
    Dim MicroDetail As Texture
    Dim BumpMap As Texture
    Dim EnvironmentMap As CubeTexture
    Dim GlowMap As Texture

    Dim PrimaryScale As Single
    Dim SecondaryScale As Single
    Dim MicroScale As Single
    Dim BumpScale As Single
    Dim GlowScale As Single

    Dim DetailFunction As IShader.DetailFunctionEnum
    Dim MicroFunction As IShader.DetailFunctionEnum

    'Animation
    Dim UAnimationFunction As Short
    Dim UAnimationPeriod As Single
    Dim UAnimationPhase As Single

    Dim VAnimationFunction As Short
    Dim VAnimationPeriod As Single
    Dim VAnimationPhase As Single

    Dim PrimaryAnimationFunction As Short
    Dim PrimaryAnimationPeriod As Single
    Dim PrimaryAnimationPhase As Single

    Dim SecondaryAnimationFunction As Short
    Dim SecondaryAnimationPeriod As Single
    Dim SecondaryAnimationPhase As Single

    Dim PlasmaAnimationFunction As Short
    Dim PlasmaAnimationPeriod As Single
    Dim PlasmaAnimationPhase As Single

    'Colors
    Dim PerpendicularBrightness As Single
    Dim ParallelBrightness As Single
    Dim PrimaryColor As Color4
    Dim SecondaryColor As Color4
    Dim PlasmaColor As Color4

    Dim Flags As Short

    Dim Type, Type2 As Short

    Sub New(ByVal senvTag As Magic.Core.Tag)

        AddHandler senvTag.TagHasChanged, AddressOf Changed

        Dim BaseIndex = senvTag("base map")
        Dim PrimaryIndex = senvTag("primary detail map")
        Dim SecondaryIndex = senvTag("secondary detail map")
        Dim MicroIndex = senvTag("micro detail map")
        Dim EnvironmentIndex = senvTag("reflection cube map")
        Dim BumpIndex = senvTag("bump map")
        Dim GlowIndex = senvTag("map")
        Flags = senvTag(12)
        Type = senvTag(14)
        Type2 = senvTag(92)

        'Textures
        If BaseIndex > -1 Then
            BaseMap = New Bitmap(magicMap.Library(BaseIndex)).Texture
        Else
            BaseMap = Globals.default2d(IShader.DefaultTextures.Additive)
        End If
        If EnvironmentIndex > -1 Then
            EnvironmentMap = New Bitmap(magicMap.Library(EnvironmentIndex)).Cube
        Else
            'EnvironmentMap = Globals.defaultCube(IShader.DefaultTextures.SignedAdditive)
        End If
        If BumpIndex > -1 Then
            BumpMap = New Bitmap(magicMap.Library(BumpIndex)).Texture
        Else
            BumpMap = Globals.default2d(IShader.DefaultTextures.Vector)
        End If
        If GlowIndex > -1 Then
            GlowMap = New Bitmap(magicMap.Library(GlowIndex)).Texture
        Else
            GlowMap = Globals.default2d(IShader.DefaultTextures.Additive)
        End If

        'Details
        If PrimaryIndex > -1 Then
            PrimaryDetail = New Bitmap(magicMap.Library(PrimaryIndex)).Texture
        End If
        If SecondaryIndex > -1 Then
            SecondaryDetail = New Bitmap(magicMap.Library(SecondaryIndex)).Texture
        End If
        If MicroIndex > -1 Then
            MicroDetail = New Bitmap(magicMap.Library(MicroIndex)).Texture
        End If

        PrimaryScale = senvTag("primary detail map scale")
        SecondaryScale = senvTag("secondary detail map scale")
        MicroScale = senvTag("micro detail map scale")
        GlowScale = senvTag("map scale")
        BumpScale = senvTag("bump map scale")

        PerpendicularBrightness = senvTag("perpendicular brightness:[0,1]")
        ParallelBrightness = senvTag("parallel brightness:[0,1]")

        PrimaryColor = New SlimDX.Color4(1, senvTag("primary on color")(0), senvTag("primary on color")(1), senvTag("primary on color")(2))
        SecondaryColor = New SlimDX.Color4(1, senvTag("secondary on color")(0), senvTag("secondary on color")(1), senvTag("secondary on color")(2))
        PlasmaColor = New SlimDX.Color4(1, senvTag("plasma on color")(0), senvTag("plasma on color")(1), senvTag("plasma on color")(2))

        PrimaryAnimationFunction = senvTag("primary animation function")
        PrimaryAnimationPeriod = senvTag("primary animation period:seconds")
        PrimaryAnimationPhase = senvTag("primary animation phase:seconds")

        SecondaryAnimationFunction = senvTag("secondary animation function")
        SecondaryAnimationPeriod = senvTag("secondary animation period:seconds")
        SecondaryAnimationPhase = senvTag("secondary animation phase:seconds")

        PlasmaAnimationFunction = senvTag("plasma animation function")
        PlasmaAnimationPeriod = senvTag("plasma animation period:seconds")
        PlasmaAnimationPhase = senvTag("plasma animation phase:seconds")


        DetailFunction = senvTag("detail map function")
        MicroFunction = senvTag("micro detail map function")

        If EnvironmentMap Is Nothing OrElse (ParallelBrightness = 0.0F AndAlso PerpendicularBrightness = 0.0F) Then
            Select Case Type
                Case 0
                    Select Case DetailFunction
                        Case IShader.DetailFunctionEnum.Multiply2X
                            Select Case MicroFunction
                                Case IShader.DetailFunctionEnum.Multiply2X
                                    Technique = senvEffect.GetTechnique("NormalDMDMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                                Case IShader.DetailFunctionEnum.Multiply
                                    Technique = senvEffect.GetTechnique("NormalDMMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.Multiplicative)
                                Case IShader.DetailFunctionEnum.Add2X
                                    Technique = senvEffect.GetTechnique("NormalDMADetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                            End Select
                        Case IShader.DetailFunctionEnum.Multiply
                            Select Case MicroFunction
                                Case IShader.DetailFunctionEnum.Multiply2X
                                    Technique = senvEffect.GetTechnique("NormalMDMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.Multiplicative, IShader.DefaultTextures.SignedAdditive)
                                Case IShader.DetailFunctionEnum.Multiply
                                    Technique = senvEffect.GetTechnique("NormalMMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.Multiplicative, IShader.DefaultTextures.SignedAdditive)
                                Case IShader.DetailFunctionEnum.Add2X
                                    Technique = senvEffect.GetTechnique("NormalMADetailBlend")
                                    SetDetail(IShader.DefaultTextures.Multiplicative, IShader.DefaultTextures.SignedAdditive)
                            End Select
                        Case IShader.DetailFunctionEnum.Add2X
                            Select Case MicroFunction
                                Case IShader.DetailFunctionEnum.Multiply2X
                                    Technique = senvEffect.GetTechnique("NormalADMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                                Case IShader.DetailFunctionEnum.Multiply
                                    Technique = senvEffect.GetTechnique("NormalAMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.Multiplicative)
                                Case IShader.DetailFunctionEnum.Add2X
                                    Technique = senvEffect.GetTechnique("NormalAADetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                            End Select
                    End Select
                Case 1, 2
                    Select Case DetailFunction
                        Case IShader.DetailFunctionEnum.Multiply2X
                            Select Case MicroFunction
                                Case IShader.DetailFunctionEnum.Multiply2X
                                    Technique = senvEffect.GetTechnique("BlendedDMDMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                                Case IShader.DetailFunctionEnum.Multiply
                                    Technique = senvEffect.GetTechnique("BlendedDMMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.Multiplicative)
                                Case IShader.DetailFunctionEnum.Add2X
                                    Technique = senvEffect.GetTechnique("BlendedDMADetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                            End Select
                        Case IShader.DetailFunctionEnum.Multiply
                            Select Case MicroFunction
                                Case IShader.DetailFunctionEnum.Multiply2X
                                    Technique = senvEffect.GetTechnique("BlendedMDMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.Multiplicative, IShader.DefaultTextures.SignedAdditive)
                                Case IShader.DetailFunctionEnum.Multiply
                                    Technique = senvEffect.GetTechnique("BlendedMMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.Multiplicative, IShader.DefaultTextures.Multiplicative)
                                Case IShader.DetailFunctionEnum.Add2X
                                    Technique = senvEffect.GetTechnique("BlendedMADetailBlend")
                                    SetDetail(IShader.DefaultTextures.Multiplicative, IShader.DefaultTextures.SignedAdditive)
                            End Select
                        Case IShader.DetailFunctionEnum.Add2X
                            Select Case MicroFunction
                                Case IShader.DetailFunctionEnum.Multiply2X
                                    Technique = senvEffect.GetTechnique("BlendedADMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                                Case IShader.DetailFunctionEnum.Multiply
                                    Technique = senvEffect.GetTechnique("BlendedAMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.Multiplicative)
                                Case IShader.DetailFunctionEnum.Add2X
                                    Technique = senvEffect.GetTechnique("BlendedAADetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                            End Select
                    End Select
            End Select
        Else
            Select Case Type2
                Case 0
                    Select Case MicroFunction
                        Case IShader.DetailFunctionEnum.Multiply2X
                            Technique = senvEffect.GetTechnique("BumpedEnvironmentMapDM")
                            SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                        Case IShader.DetailFunctionEnum.Multiply
                            Technique = senvEffect.GetTechnique("BumpedEnvironmentMapM")
                            SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.Multiplicative)
                        Case IShader.DetailFunctionEnum.Add2X
                            Technique = senvEffect.GetTechnique("BumpedEnvironmentMapA")
                            SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                    End Select
                Case 1
                    Select Case MicroFunction
                        Case IShader.DetailFunctionEnum.Multiply2X
                            Technique = senvEffect.GetTechnique("FlatEnvironmentMapDM")
                            SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                        Case IShader.DetailFunctionEnum.Multiply
                            Technique = senvEffect.GetTechnique("FlatEnvironmentMapM")
                            SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.Multiplicative)
                        Case IShader.DetailFunctionEnum.Add2X
                            Technique = senvEffect.GetTechnique("FlatEnvironmentMapA")
                            SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                    End Select

            End Select
        End If

    End Sub

    Sub Changed(ByVal senvTag As Magic.Core.Tag)

        Dim BaseIndex = senvTag("base map")
        Dim PrimaryIndex = senvTag("primary detail map")
        Dim SecondaryIndex = senvTag("secondary detail map")
        Dim MicroIndex = senvTag("micro detail map")
        Dim EnvironmentIndex = senvTag("reflection cube map")
        Dim BumpIndex = senvTag("bump map")
        Dim GlowIndex = senvTag("map")
        Flags = senvTag(12)
        Type = senvTag(14)
        Type2 = senvTag(92)

        'Textures
        If BaseIndex > -1 Then
            BaseMap = New Bitmap(magicMap.Library(BaseIndex)).Texture
        Else
            BaseMap = Globals.default2d(IShader.DefaultTextures.Additive)
        End If
        If EnvironmentIndex > -1 Then
            EnvironmentMap = New Bitmap(magicMap.Library(EnvironmentIndex)).Cube
        Else
            EnvironmentMap = Globals.defaultCube(IShader.DefaultTextures.SignedAdditive)
        End If
        If BumpIndex > -1 Then
            BumpMap = New Bitmap(magicMap.Library(BumpIndex)).Texture
        Else
            BumpMap = Globals.default2d(IShader.DefaultTextures.Vector)
        End If
        If GlowIndex > -1 Then
            GlowMap = New Bitmap(magicMap.Library(GlowIndex)).Texture
        Else
            GlowMap = Globals.default2d(IShader.DefaultTextures.Additive)
        End If

        'Details
        If PrimaryIndex > -1 Then
            PrimaryDetail = New Bitmap(magicMap.Library(PrimaryIndex)).Texture
        End If
        If SecondaryIndex > -1 Then
            SecondaryDetail = New Bitmap(magicMap.Library(SecondaryIndex)).Texture
        End If
        If MicroIndex > -1 Then
            MicroDetail = New Bitmap(magicMap.Library(MicroIndex)).Texture
        End If

        PrimaryScale = senvTag("primary detail map scale")
        SecondaryScale = senvTag("secondary detail map scale")
        MicroScale = senvTag("micro detail map scale")
        GlowScale = senvTag("map scale")
        BumpScale = senvTag("bump map scale")

        PerpendicularBrightness = senvTag("perpendicular brightness:[0,1]")
        ParallelBrightness = senvTag("parallel brightness:[0,1]")

        PrimaryColor = New SlimDX.Color4(1, senvTag("primary on color")(0), senvTag("primary on color")(1), senvTag("primary on color")(2))
        SecondaryColor = New SlimDX.Color4(1, senvTag("secondary on color")(0), senvTag("secondary on color")(1), senvTag("secondary on color")(2))
        PlasmaColor = New SlimDX.Color4(1, senvTag("plasma on color")(0), senvTag("plasma on color")(1), senvTag("plasma on color")(2))

        PrimaryAnimationFunction = senvTag("primary animation function")
        PrimaryAnimationPeriod = senvTag("primary animation period:seconds")
        PrimaryAnimationPhase = senvTag("primary animation phase:seconds")

        SecondaryAnimationFunction = senvTag("secondary animation function")
        SecondaryAnimationPeriod = senvTag("secondary animation period:seconds")
        SecondaryAnimationPhase = senvTag("secondary animation phase:seconds")

        PlasmaAnimationFunction = senvTag("plasma animation function")
        PlasmaAnimationPeriod = senvTag("plasma animation period:seconds")
        PlasmaAnimationPhase = senvTag("plasma animation phase:seconds")


        DetailFunction = senvTag("detail map function")
        MicroFunction = senvTag("micro detail map function")

        If EnvironmentMap Is Nothing OrElse (ParallelBrightness = 0.0F AndAlso PerpendicularBrightness = 0.0F) Then
            Select Case Type
                Case 0
                    Select Case DetailFunction
                        Case IShader.DetailFunctionEnum.Multiply2X
                            Select Case MicroFunction
                                Case IShader.DetailFunctionEnum.Multiply2X
                                    Technique = senvEffect.GetTechnique("NormalDMDMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                                Case IShader.DetailFunctionEnum.Multiply
                                    Technique = senvEffect.GetTechnique("NormalDMMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.Multiplicative)
                                Case IShader.DetailFunctionEnum.Add2X
                                    Technique = senvEffect.GetTechnique("NormalDMADetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                            End Select
                        Case IShader.DetailFunctionEnum.Multiply
                            Select Case MicroFunction
                                Case IShader.DetailFunctionEnum.Multiply2X
                                    Technique = senvEffect.GetTechnique("NormalMDMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.Multiplicative, IShader.DefaultTextures.SignedAdditive)
                                Case IShader.DetailFunctionEnum.Multiply
                                    Technique = senvEffect.GetTechnique("NormalMMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.Multiplicative, IShader.DefaultTextures.SignedAdditive)
                                Case IShader.DetailFunctionEnum.Add2X
                                    Technique = senvEffect.GetTechnique("NormalMADetailBlend")
                                    SetDetail(IShader.DefaultTextures.Multiplicative, IShader.DefaultTextures.SignedAdditive)
                            End Select
                        Case IShader.DetailFunctionEnum.Add2X
                            Select Case MicroFunction
                                Case IShader.DetailFunctionEnum.Multiply2X
                                    Technique = senvEffect.GetTechnique("NormalADMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                                Case IShader.DetailFunctionEnum.Multiply
                                    Technique = senvEffect.GetTechnique("NormalAMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.Multiplicative)
                                Case IShader.DetailFunctionEnum.Add2X
                                    Technique = senvEffect.GetTechnique("NormalAADetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                            End Select
                    End Select
                Case 1, 2
                    Select Case DetailFunction
                        Case IShader.DetailFunctionEnum.Multiply2X
                            Select Case MicroFunction
                                Case IShader.DetailFunctionEnum.Multiply2X
                                    Technique = senvEffect.GetTechnique("BlendedDMDMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                                Case IShader.DetailFunctionEnum.Multiply
                                    Technique = senvEffect.GetTechnique("BlendedDMMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.Multiplicative)
                                Case IShader.DetailFunctionEnum.Add2X
                                    Technique = senvEffect.GetTechnique("BlendedDMADetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                            End Select
                        Case IShader.DetailFunctionEnum.Multiply
                            Select Case MicroFunction
                                Case IShader.DetailFunctionEnum.Multiply2X
                                    Technique = senvEffect.GetTechnique("BlendedMDMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.Multiplicative, IShader.DefaultTextures.SignedAdditive)
                                Case IShader.DetailFunctionEnum.Multiply
                                    Technique = senvEffect.GetTechnique("BlendedMMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.Multiplicative, IShader.DefaultTextures.Multiplicative)
                                Case IShader.DetailFunctionEnum.Add2X
                                    Technique = senvEffect.GetTechnique("BlendedMADetailBlend")
                                    SetDetail(IShader.DefaultTextures.Multiplicative, IShader.DefaultTextures.SignedAdditive)
                            End Select
                        Case IShader.DetailFunctionEnum.Add2X
                            Select Case MicroFunction
                                Case IShader.DetailFunctionEnum.Multiply2X
                                    Technique = senvEffect.GetTechnique("BlendedADMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                                Case IShader.DetailFunctionEnum.Multiply
                                    Technique = senvEffect.GetTechnique("BlendedAMDetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.Multiplicative)
                                Case IShader.DetailFunctionEnum.Add2X
                                    Technique = senvEffect.GetTechnique("BlendedAADetailBlend")
                                    SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                            End Select
                    End Select
            End Select
        Else
            Select Case Type2
                Case 0
                    Select Case MicroFunction
                        Case IShader.DetailFunctionEnum.Multiply2X
                            Technique = senvEffect.GetTechnique("BumpedEnvironmentMapDM")
                            SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                        Case IShader.DetailFunctionEnum.Multiply
                            Technique = senvEffect.GetTechnique("BumpedEnvironmentMapM")
                            SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.Multiplicative)
                        Case IShader.DetailFunctionEnum.Add2X
                            Technique = senvEffect.GetTechnique("BumpedEnvironmentMapA")
                            SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                    End Select
                Case 1
                    Select Case MicroFunction
                        Case IShader.DetailFunctionEnum.Multiply2X
                            Technique = senvEffect.GetTechnique("FlatEnvironmentMapDM")
                            SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                        Case IShader.DetailFunctionEnum.Multiply
                            Technique = senvEffect.GetTechnique("FlatEnvironmentMapM")
                            SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.Multiplicative)
                        Case IShader.DetailFunctionEnum.Add2X
                            Technique = senvEffect.GetTechnique("FlatEnvironmentMapA")
                            SetDetail(IShader.DefaultTextures.SignedAdditive, IShader.DefaultTextures.SignedAdditive)
                    End Select

            End Select
        End If

    End Sub
    Private Sub SetDetail(ByVal defaultIndex As Integer, ByVal defaultMIndex As Integer)
        If (PrimaryDetail Is Nothing) Then
            PrimaryDetail = Globals.default2d(defaultIndex)
        End If
        If (SecondaryDetail Is Nothing) Then
            SecondaryDetail = Globals.default2d(defaultIndex)
        End If
        If (MicroDetail Is Nothing) Then
            MicroDetail = Globals.default2d(defaultMIndex)
        End If
    End Sub
    Public Sub BeginPass(ByVal pass As Integer) Implements IShader.BeginPass
        If (pass = 0) Then
            xDevice.SetRenderState(RenderState.AlphaBlendEnable, False)
            If ((Flags And 1) = 0) Then
                xDevice.SetRenderState(RenderState.AlphaTestEnable, False)
            Else
                xDevice.SetRenderState(RenderState.AlphaTestEnable, True)
                xDevice.SetRenderState(RenderState.AlphaRef, 127)
                xDevice.SetRenderState(RenderState.AlphaFunc, Compare.GreaterEqual)
            End If
            xDevice.SetRenderState(RenderState.CullMode, Cull.Counterclockwise)
        End If
        senvEffect.BeginPass(pass)
    End Sub
    Public Sub EndPass() Implements IShader.EndPass
        senvEffect.EndPass()
    End Sub
    Public Function BeginShader(ByVal tLightmap As Texture) As Integer Implements IShader.BeginShader
        'Globals
        senvEffect.SetValue("cameraPosition", New Vector4(Camera.Position.X, Camera.Position.Y, Camera.Position.Z, 1.0F))
        senvEffect.SetValue("world", xDevice.GetTransform(TransformState.World))
        senvEffect.SetValue("worldViewProjection", xDevice.GetTransform(TransformState.World) * xDevice.GetTransform(TransformState.View) * xDevice.GetTransform(TransformState.Projection))
        senvEffect.SetValue("fogStart", xDevice.GetRenderState(RenderState.FogStart)) '0.0F) ' MdxRender.Device.RenderState.FogStart);
        senvEffect.SetValue("fogEnd", xDevice.GetRenderState(RenderState.FogEnd)) ' MdxRender.Device.RenderState.FogEnd);
        senvEffect.SetValue("fogDensity", xDevice.GetRenderState(RenderState.FogDensity)) ' MdxRender.Device.RenderState.FogDensity);
        senvEffect.SetValue("ambientColor", New Color4(Color.White))

        'Textures
        senvEffect.SetTexture("BaseMap", BaseMap)
        senvEffect.SetTexture("DetailMapA", PrimaryDetail)
        senvEffect.SetTexture("DetailMapB", SecondaryDetail)
        senvEffect.SetTexture("MicroDetailMap", MicroDetail)
        senvEffect.SetTexture("EnvironmentMap", EnvironmentMap)
        senvEffect.SetTexture("BumpMap", BumpMap)
        senvEffect.SetTexture("GlowMap", GlowMap)
        senvEffect.SetTexture("VectorNormalization", Globals.vectorNormalization(IShader.DefaultTextures.Additive))
        If tLightmap Is Nothing Then
            senvEffect.SetTexture("LightMap", Globals.default2d(IShader.DefaultTextures.Multiplicative))
        Else
            senvEffect.SetTexture("LightMap", tLightmap)
        End If

        'Scales
        senvEffect.SetValue("bumpScale", BumpScale)
        senvEffect.SetValue("glowScale", GlowScale)
        senvEffect.SetValue("detailAScale", PrimaryScale)
        senvEffect.SetValue("detailBScale", SecondaryScale)
        senvEffect.SetValue("microDetailScale", MicroScale)

        'Colors
        senvEffect.SetValue("parallelShininess", ParallelBrightness)
        senvEffect.SetValue("perpendicularShininess", PerpendicularBrightness)
        senvEffect.SetValue("primaryAnimationColor", PrimaryColor)
        senvEffect.SetValue("secondaryAnimationColor", SecondaryColor)
        senvEffect.SetValue("plasmaAnimationColor", PlasmaColor)

        'Animation
        senvEffect.SetValue("uAnimationValue", Animate(UAnimationFunction, UAnimationPeriod, 1))
        senvEffect.SetValue("vAnimationValue", Animate(VAnimationFunction, VAnimationPeriod, 1))
        senvEffect.SetValue("primaryAnimationValue", Animate(PrimaryAnimationFunction, PrimaryAnimationPeriod, 1))
        senvEffect.SetValue("secondaryAnimationValue", Animate(SecondaryAnimationFunction, SecondaryAnimationPeriod, 1))
        senvEffect.SetValue("plasmaAnimationValue", Animate(PlasmaAnimationFunction, PlasmaAnimationPeriod, 1))
        'senvEffect.SetValue("uAnimationValue", Animate(UAnimationFunction, UAnimationPeriod, UAnimationPhase))
        'senvEffect.SetValue("vAnimationValue", Animate(VAnimationFunction, VAnimationPeriod, VAnimationPhase))
        'senvEffect.SetValue("primaryAnimationValue", Animate(PrimaryAnimationFunction, PrimaryAnimationPeriod, PrimaryAnimationPhase))
        'senvEffect.SetValue("secondaryAnimationValue", Animate(SecondaryAnimationFunction, SecondaryAnimationPeriod, SecondaryAnimationPhase))
        'senvEffect.SetValue("plasmaAnimationValue", Animate(PlasmaAnimationFunction, PlasmaAnimationPeriod, PlasmaAnimationPhase))


        senvEffect.Technique = Technique

        Return senvEffect.Begin(FX.DoNotSaveState)
    End Function
    Public Sub EndShader() Implements IShader.EndShader
        senvEffect.End()
    End Sub
End Class
